package consoleUi;

import java.io.*;
import java.net.DatagramSocket;
import java.net.DatagramPacket;

import config.PSG_Configuration;

public class NotificationReceiver {
  public static void main (String[] args){
	  int i=0;
	  try{
		  int receiverPort = PSG_Configuration.defaultPort+10;
		  DatagramSocket socket = new DatagramSocket(receiverPort);
		  while(true){
			  i++;
			  byte data[] = new byte[100];
			  DatagramPacket notification = new DatagramPacket(data,data.length);
			  socket.receive(notification);
			  System.out.println("[Notification number]" + i);
			  System.out.println("[Receive Notification From]: "+notification.getAddress());
			  System.out.println("[Receive Notification From Port]: "+notification.getPort());
			  System.out.println("[Receive Notification]: "+ new String(notification.getData(),0,notification.getLength()));
			  
			  String response = "[Notification has been received successfully!]";
			  byte responseData[] = response.getBytes();
			  DatagramPacket responsePacket = new DatagramPacket(responseData,responseData.length,notification.getAddress(),notification.getPort());
			  socket.send(responsePacket);
//			  socket.close();
		  }
	  }catch (IOException e){
		  
	  }
  }
}
